#/*************************************************************************
#    > File Name: calc/calc_setting.py
#    > Author: Yan Wang
#    > Mail: wangyan@imnu.edu.cn
#    > Created Time: 2022年07月21日 星期四 14时31分39秒
# ************************************************************************/
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import paramiko 
sys.path.append('../Lib')
from Load_Lib import * 
from event_path import * 

# 设置print的显示规则
np.set_printoptions(threshold=np.inf)
pd.set_option('display.max_columns',None)
pd.set_option('display.max_rows',None)


# 设置基本的工作路径
param_dir=Work_Param_Init(process)

# 设置parton level 截面计算参数
theoretical_para_file_name=param_dir["input_folder"]+"/data/data_grid_{fname}.tsv".format(fname=process["kind"])
param_df=Load_THDM_Param(theoretical_para_file_name)
param_length=len(param_df)


Set_Process_Para_Plot(process, param_df)

# 设置MG param card的参数
param_source = param_dir["input_folder"]+'/card/param_card.dat'
if process["mode"]=="fixed_width":
	MGparam_label_value={"Block frblock":2,"23 9.118760e+01 # MZ ":4}
	MGparam_replace_value={"tb" :"    1 scan1: {} # TB",
						  "sba" :"    2 scan1: {} # sinbma",
						  "mh"  :"   25 scan1: {} # mh1",
						  "mH"  :"   35 scan1: {} # mh2",
						  "mA"  :"   36 scan1: {} # mh3",
						  "mch" :"   37 scan1: {} # mhc"}
	MGparam_label_constant={"DECAY  24 2.085000e+00 # WW ":4}
	MGparam_replace_constant={"wh"  :"DECAY  25 Auto # Wh1 ",
							  "wH"  :"DECAY  35 Auto # Wh1 ",
							  "wA"  :"DECAY  36 Auto # Wh1 ",
							  "wch" :"DECAY  37 Auto# Wh1 "}
elif process["mode"]=="variable_width":
	MGparam_label_value={"Block frblock":2,"23 9.118760e+01 # MZ ":4,"DECAY  24 2.085000e+00 # WW ":4}
	MGparam_replace_value={"tb" :"    1 scan1: {} # TB",
						  "sba" :"    2 scan1: {} # sinbma",
						  "mh"  :"   25 scan1: {} # mh1",
						  "mH"  :"   35 scan1: {} # mh2",
						  "mA"  :"   36 scan1: {} # mh3",
						  "mch" :"   37 scan1: {} # mhc",
						  "wh"  :"DECAY  25 Auto # Wh ",
						  "wH"  :"DECAY  35 Auto # WH ",
						  "wA"  :"DECAY  36 Auto # WA ",
						  "wch" :"DECAY  37 Auto # Wch "}
elif param_mode=="seperate":
	MGparam_label_value={"Block frblock":2,"23 9.118760e+01 # MZ ":4}
	MGparam_replace_value={"tb":"      1 {} # TB",
			              "sba" :"    2 {} # sinbma",
				          "mh"  :"   25 {} # mh1",
					      "mH"  :"   35 {} # mh2",
						  "mA"  :"   36 {} # mh3",
						  "mch" :"   37 {} # mhc"}
	MGparam_label_constant={"DECAY  24 2.085000e+00 # WW ":4}
	MGparam_replace_constant={"wh"  :"DECAY  25 Auto # Wh1 ",
							  "wH"  :"DECAY  35 Auto # Wh1 ",
							  "wA"  :"DECAY  36 Auto # Wh1 ",
							  "wch" :"DECAY  37 Auto# Wh1 "}
scan_folder="scan_param_{fname}/".format(fname=process["process_event_kind"])
param_general_file_name=param_dir["output_folder"]+scan_folder+'/param_card_{}.dat'






if process["pt"]=="10":
	MG_run_card=param_dir["input_folder"]+"/card/run_card_10.dat"
	MG_delphes_card=param_dir["input_folder"]+"/card/delphes_card_10.dat"
elif process["pt"]=="20":
	MG_run_card=param_dir["input_folder"]+"/card/run_card_20.dat"
	MG_delphes_card=param_dir["input_folder"]+"/card/delphes_card_20.dat"




# 设置MG工作路径
event_folder="Events_{fname}".format(fname=process["event_kind"])
MG_parallel_output_folder=param_dir["output_folder"]+"parallel_tmp/mg_results_{fname}/".format(fname=process["process_event_kind"])


# 对于考虑 cut 的事例
folder_num=Set_Process_Para_Folder_Num(process,param_dir["node_num"],param_length)

MG_original_folder=param_dir["MG"]+"{fprocess}".format(fprocess=process["name"])
MG_split_event_folder=[]
MG_combined_event_folder=param_dir["MG"]+event_folder+"/{fprocess}_0/Events/".format(fprocess=process["name"])
for num in range(folder_num):
	new_folder=param_dir["MG"]+event_folder+"/{fprocess}_{fn}/".format(fprocess=process["name"],fn=num+1)
	MG_split_event_folder.append(new_folder)


# 对于考虑 no cut 的事例
event_folder_nocut="Events_{fname}_nocut".format(fname=process["kind"])
MG_original_folder_nocut=param_dir["MG"]+"/{fprocess}_nocut/Events/".format(fprocess=process["name"])
MG_combined_event_folder_nocut=param_dir["MG"]+event_folder_nocut+"/{fprocess}_0/Events/".format(fprocess=process["name"])
MG_split_event_folder_nocut=[]
for num in range(folder_num):
	new_folder=param_dir["MG"]+event_folder_nocut+"/{fprocess}_nocut_{fn}/".format(fprocess=process["name"],fn=num+1)
	MG_split_event_folder_nocut.append(new_folder)


scan_general_file_name="scan_run_[01-{fnum}]_mycode.txt"

# 设置parton level截面与其他输入参数作为hotmap的参数。 
MG_hotmap_out_file=param_dir["output_folder"]+"/xection/{fname}_for_MG_xection.dat".format(fname=process["process_event_kind"])
MG_hotmap_variable_name="Parton_xection"
MG_hotmap_file_name=param_dir["output_folder"]+"/hotmap/{fname}_for_MG_xection.C".format(fname=process["process_event_kind"])
MG_param_out_file=param_dir["output_folder"]+"/param/{fname}_for_MG.dat".format(fname=process["process_event_kind"])

MG_para_plot={	"file_name": MG_hotmap_file_name,
		"bins":process["plot"]["bins"],
		"data_length":param_length,
		"var_name":MG_hotmap_variable_name,
		"X_name": process["plot"]["xname"],
		"Y_name": process["plot"]["yname"],
		}

# 总结默认MG参数
MG_para={}
MG_para["MG_dir"]=param_dir["MG"]
MG_para["work_dir"]=param_dir["work"]
MG_para["ARCH"]=param_dir["ARCH"]
MG_para["BASDA"]=param_dir["BASDA"]
MG_para["replace_value"]=MGparam_replace_value
MG_para["data_df"]=param_df
MG_para["label_value"]=MGparam_label_value
MG_para["node_num"]=param_dir["node_num"]
MG_para["folder_num"]=folder_num
MG_para["node_name"]=param_dir["node_name"]
MG_para["length"]=param_length
MG_para["source"]=param_source
MG_para["general_file_name"]=param_general_file_name
MG_para["run_card"]=MG_run_card
MG_para["delphes_card"]=MG_delphes_card
MG_para["record_file"]=MG_parallel_output_folder+"mg_{}"
MG_para["scan_file_general_name"]=scan_general_file_name

MG_para["original_folder"]=MG_original_folder
MG_para["split_event_folder"]=MG_split_event_folder
MG_para["combined_event_folder"]=MG_combined_event_folder
MG_para["param_out_file"]=MG_param_out_file
MG_para["combined_scan_file"]=MG_para["combined_event_folder"]+scan_general_file_name
MG_para["split_scan_file"]=[this_file+scan_general_file_name for this_file in MG_split_event_folder]

MG_para["original_folder_nocut"]=MG_original_folder_nocut
MG_para["split_event_folder_nocut"]=MG_split_event_folder_nocut
MG_para["combined_event_folder_nocut"]=MG_combined_event_folder_nocut
MG_para["combined_scan_file_nocut"]=MG_para["combined_event_folder_nocut"]+scan_general_file_name
MG_para["split_scan_file_nocut"]=[this_file+scan_general_file_name for this_file in MG_split_event_folder_nocut]
MG_para["param_out_file_nocut"]=MG_param_out_file+"_nocut"



# 设置ARCH相关参数
event_file_data=param_dir["example"]+"test_input/param_file.dat"
param_ARCH=Param_ARCH_Control(param_dir,process,event_file_data,MG_para)
ARCH_dir=Param_ARCH_Dir(param_dir,process,MG_para)


ARCH_cut_file_name  =MG_combined_event_folder      +scan_general_file_name.format(fnum=param_length)
ARCH_nocut_file_name=MG_combined_event_folder_nocut+scan_general_file_name.format(fnum=param_length)
ARCH_parallel_output_folder=param_dir["output_folder"]+"parallel_tmp/ARCH_results_{fname}/".format(fname=process["process_event_kind"])


# 设置BASDA相关参数，利用ARCH输出结果

BASDA_control_template= './examples/test_input/control_BASDA'
BASDA_target_control_folder= param_dir["BASDA"]+'/control/control_scan_{}'.format(process["process_event_kind"])
BASDA_event_general_folder="events/Event_Analyse_scan_{fname}"
BASDA_control_dir={"source":BASDA_control_template,"target":BASDA_target_control_folder}

BASDA_para_path={"origin": {"process":"PROCESS                   : {}_$fnum".format(process["process_event_kind"]),
			"out_folder" :"OUTPUT_FOLDER    : "+BASDA_event_general_folder.format(fname=process["process_event_kind"])+"_$fnum",
			    "tmp" :"RECORD_FILE               : \"tmp/{}_$fnum.dat\"".format(process["process_event_kind"]),
			"in_folder" :"INPUT_FOLDER     : './control/control_scan_{}'".format(process["process_event_kind"]), #这里要和上面定义的ARCH_target_control_folder对应
			    "file":"INPUT_FILE                : \"file_$fnum.dat\""},
		"source":"path.dat",
		"labels":{"PROCESS                   : 10_bp3_4b0j_1":0,
                    "OUTPUT_FOLDER             : 'events/Event_scan_1'":0,
                    "RECORD_FILE               : 'tmp/tmp_h_w4b_1.dat'":0, 
		    "INPUT_FOLDER              : './control/control_scan_4b0j'":0,
                    "INPUT_FILE                : 'file_1.dat'":0}
			}

BASDA_para_file={"origin": {"file":"FILE_0            : xsection_$fnum.dat"},
			"source":"file.dat",
			"labels":{"FILE_0            : xsection_1.dat":0}
			}
BASDA_para_xection={"origin": {"file":"        "+ARCH_dir["tmp_folder"]+"events/sigh_w4b_$fnum.root        : $fvalue "},
			"source":"xsection.dat",
			"labels":{"        file  : 1.554":0}
			}


BASDA_replaced_para={"path":BASDA_para_path, 
		"file":BASDA_para_file,
		"xection":BASDA_para_xection}

# 提取最终significance，利用BASDA的输出结果
BASDA_out_name = param_dir["BASDA"]+BASDA_event_general_folder.format(fname=process["process_event_kind"])+"_{}"+"/data/125/output_table_combined_125.dat"
BASDA_hotmap_out_file=param_dir["output_folder"]+"/xection/{fname}_for_BASDA_significance.dat".format(fname=process["process_event_kind"])
BASDA_hotmap_variable_name="sig_data"
BASDA_hotmap_file_name=param_dir["output_folder"]+"/hotmap/{fname}_for_BASDA_significance.C".format(fname=process["process_event_kind"])
BASDA_param_out_file=param_dir["output_folder"]+"/param/{fname}_for_BASDA.dat".format(fname=process["process_event_kind"])


BASDA_parallel_output_folder=param_dir["output_folder"]+"parallel_tmp/BASDA_results_{fname}/".format(fname=process["process_event_kind"])

